📑   Links

In [ ]:

📑   Library

In [1]:
R.version.string
'R version 3.3.2 (2016-10-31)'
In [11]:
library(repr)
In [12]:
library(ggplot2)
library(DT)
library(formattable)
library(plotly)
In [13]:
library(osmar)
library(maptools)
library(ggmap)
In [14]:
library(igraph)
library(leaflet)
In [15]:
library(networkD3)
library(RColorBrewer)
library(plotrix)
In [16]:
library(highcharter)
library(visNetwork)
library(d3heatmap)
In [8]:
ip <- as.data.frame(installed.packages()[,c(1,3:4)])
rownames(ip) <- NULL
ip <- ip[is.na(ip$Priority),1:2,drop=FALSE]
In [10]:
tail(ip,10)
PackageVersion
243whisker 0.3-2
244withr 1.0.2
245xgboost 0.6-4
246xlsxjars0.6.1
247XML 3.98-1.5
248xml2 1.1.1
249xtable 1.8-2
250xts 0.9-7
251yaml 2.1.14
252zoo 1.7-14
In [ ]:

In [ ]:

📑   Data

In [18]:
gc01 <- geocode("GUM Moscow", output = "more")
t(data.frame(gc01))
Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=GUM%20Moscow&sensor=false
lon37.62152
lat55.7547
typeestablishment
loctypeapproximate
addressred square, 3, moskva, russia, 101000
north55.75605
south55.75335
east37.62287
west37.62017
street_number3
routeRed Square
politicalTsentralnyy administrativnyy okrug
localityMoskva
administrative_area_level_2gorod Moskva
countryRussia
postal_code101000
In [16]:
formattable(data.frame(mapdist("GUM Moscow", "Bolshoy Theater")))
by using this function you are agreeing to the terms at :
http://code.google.com/apis/maps/documentation/distancematrix/

Information from URL : http://maps.googleapis.com/maps/api/distancematrix/json?origins=GUM+Moscow&destinations=Bolshoy+Theater&mode=driving&sensor=false
fromtomkmmilessecondsminuteshours
GUM Moscow Bolshoy Theater6093 6.093 3.78619 1135 18.91667 0.3152778
In [17]:
src <- osmsource_api()
bigbox <- center_bbox(38.6533, 55.7758, 3000, 3000)
bpavposad <- get_osm(bigbox, source = src)
str(bpavposad)
List of 3
 $ nodes    :List of 2
  ..$ attrs:'data.frame':	27411 obs. of  9 variables:
  .. ..$ id       : num [1:27411] 7.65e+07 2.54e+09 2.54e+09 2.54e+09 2.54e+09 ...
  .. ..$ visible  : Factor w/ 1 level "true": 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ timestamp: POSIXlt[1:27411], format: "2013-03-22 17:51:04" "2013-11-21 21:54:26" ...
  .. ..$ version  : num [1:27411] 3 1 1 1 1 1 1 1 1 1 ...
  .. ..$ changeset: num [1:27411] 15457006 19045860 19045860 19045860 19046068 ...
  .. ..$ user     : Factor w/ 67 levels "347929","Владимир К",..: 42 29 29 29 29 29 29 29 29 29 ...
  .. ..$ uid      : Factor w/ 67 levels "109327","1101663",..: 43 14 14 14 14 14 14 14 14 14 ...
  .. ..$ lat      : num [1:27411] 55.8 55.8 55.8 55.8 55.8 ...
  .. ..$ lon      : num [1:27411] 38.7 38.7 38.7 38.6 38.7 ...
  ..$ tags :'data.frame':	987 obs. of  3 variables:
  .. ..$ id: num [1:987] 2.54e+09 2.54e+09 2.54e+09 2.54e+09 1.34e+09 ...
  .. ..$ k : Factor w/ 100 levels "addr:city","addr:country",..: 21 33 21 33 21 33 21 33 91 21 ...
  .. ..$ v : Factor w/ 344 levels "+7 496 4322013",..: 334 245 334 245 334 245 334 245 339 334 ...
  ..- attr(*, "class")= chr [1:3] "nodes" "osmar_element" "list"
 $ ways     :List of 3
  ..$ attrs:'data.frame':	6082 obs. of  7 variables:
  .. ..$ id       : num [1:6082] 20272219 23323556 23323558 23323560 37783843 ...
  .. ..$ visible  : Factor w/ 1 level "true": 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ timestamp: POSIXlt[1:6082], format: "2015-07-23 11:41:57" "2016-06-22 14:07:50" ...
  .. ..$ version  : num [1:6082] 10 18 6 6 22 7 12 15 7 8 ...
  .. ..$ changeset: num [1:6082] 32823720 40206486 22104907 20303245 46424640 ...
  .. ..$ user     : Factor w/ 62 levels "Павел Гетманцев",..: 28 35 40 50 59 23 33 23 33 40 ...
  .. ..$ uid      : Factor w/ 62 levels "109327","1101663",..: 44 60 39 46 58 36 33 36 33 39 ...
  ..$ tags :'data.frame':	7917 obs. of  3 variables:
  .. ..$ id: num [1:7917] 20272219 20272219 20272219 20272219 20272219 ...
  .. ..$ k : Factor w/ 97 levels "addr:city","addr:country",..: 28 29 34 35 63 67 73 74 91 93 ...
  .. ..$ v : Factor w/ 476 levels "-1","\"Ветерок\"",..: 365 476 5 25 476 41 428 6 400 66 ...
  ..$ refs :'data.frame':	40884 obs. of  2 variables:
  .. ..$ id : num [1:40884] 20272219 20272219 20272219 23323556 23323556 ...
  .. ..$ ref: num [1:40884] 1.30e+09 1.34e+09 1.34e+09 2.54e+08 1.34e+09 ...
  ..- attr(*, "class")= chr [1:3] "ways" "osmar_element" "list"
 $ relations:List of 3
  ..$ attrs:'data.frame':	64 obs. of  7 variables:
  .. ..$ id       : num [1:64] 179639 1693608 3850329 1638020 1638017 ...
  .. ..$ visible  : Factor w/ 1 level "true": 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ timestamp: POSIXlt[1:64], format: "2017-01-17 08:34:38" "2016-11-17 20:20:16" ...
  .. ..$ version  : num [1:64] 18 5 26 3 1 1 1 1 1 4 ...
  .. ..$ changeset: num [1:64] 45236534 43742492 46963072 18332876 8541357 ...
  .. ..$ user     : Factor w/ 17 levels "Alexander-II",..: 8 14 3 7 9 9 9 12 12 12 ...
  .. ..$ uid      : Factor w/ 17 levels "1101663","1232315",..: 15 7 3 17 6 6 6 1 1 1 ...
  ..$ tags :'data.frame':	308 obs. of  3 variables:
  .. ..$ id: num [1:308] 179639 179639 179639 179639 179639 ...
  .. ..$ k : Factor w/ 105 levels "addr:country",..: 1 3 5 6 9 14 20 90 103 104 ...
  .. ..$ v : Factor w/ 145 levels "007Е;008Е","036Э",..: 128 58 16 66 77 13 37 66 80 120 ...
  ..$ refs :'data.frame':	7996 obs. of  4 variables:
  .. ..$ id  : num [1:7996] 179639 179639 179639 179639 179639 ...
  .. ..$ type: Factor w/ 3 levels "node","relation",..: 3 3 3 3 3 3 3 3 3 3 ...
  .. ..$ ref : num [1:7996] 1.24e+08 1.24e+08 1.24e+08 1.24e+08 1.24e+08 ...
  .. ..$ role: Factor w/ 16 levels "","admin_centre",..: 9 9 9 9 9 9 9 9 9 9 ...
  ..- attr(*, "class")= chr [1:3] "relations" "osmar_element" "list"
 - attr(*, "class")= chr [1:2] "osmar" "list"
In [18]:
users <- sort(unique(bpavposad$nodes$attrs$user))
print(head(users, 12))
 [1] 347929               Владимир К           Наталья Филаретова  
 [4] Павел Гетманцев      Челентано            a_isaeva            
 [7] abel801              ad47                 adekvatno           
[10] alex_konstantino     Alexander Leschinsky AMDmi3              
67 Levels: 347929 Владимир К Наталья Филаретова Павел Гетманцев ... Zloydevol
In [21]:
n <- replicate(20, rnorm(20))
In [22]:
n_data = data.frame(n)
In [23]:
A <- matrix(
    c(0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
      1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
      1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0,
      0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0,
      0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0,
      0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
      0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0), 
    nrow = 15,               
    ncol = 15,               
    byrow = TRUE)
In [24]:
l2 <- c('Rim', 'Other', 'Apple', 'Palm', 'Motorola', 'Nokia')
x2 <-  c(39.0, 21.2, 19.5, 9.8, 7.4, 3.1)

📑   Maps

In [24]:
pp_gmap <- get_map(location=c(lon = 38.6533, lat = 55.7758), 
                      source = "google", maptype = "hybrid", zoom = 15)
ggmap(pp_gmap, extent = "normal")
Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=55.7758,38.6533&zoom=15&size=640x640&scale=2&maptype=hybrid&language=en-EN&sensor=false
In [25]:
var_ways <- route('GUM Moscow', 'Bolshoy Theater', alternatives = TRUE)
t(head(data.frame(var_ways)))
Information from URL : http://maps.googleapis.com/maps/api/directions/json?origin=GUM+Moscow&destination=Bolshoy+Theater&mode=driving&units=metric&alternatives=true&sensor=false
123456
m 42 264 584 262 74 452
km0.042 0.264 0.584 0.262 0.074 0.452
miles0.0260988 0.1640496 0.3628976 0.1628068 0.0459836 0.2808728
seconds 13 53 64 41 10 126
minutes0.2166667 0.8833333 1.0666667 0.6833333 0.1666667 2.1000000
hours0.0036111110.0147222220.0177777780.0113888890.0027777780.035000000
startLon37.62317 37.62262 37.62409 37.62470 37.62061 37.62016
startLat55.75391 55.75369 55.75157 55.74634 55.74586 55.74646
endLon37.62262 37.62409 37.62470 37.62061 37.62016 37.61426
endLat55.75369 55.75157 55.74634 55.74586 55.74646 55.74457
leg1 2 3 4 5 6
routeA A A A A A
In [26]:
options(repr.plot.width=6, repr.plot.height=4)
map1 <- ggplot(data = var_ways) + coord_map() +
  geom_leg(aes(x = startLon, xend = endLon, y = startLat, yend = endLat, color = route))
map1
In [27]:
options(repr.plot.width=14, repr.plot.height=5)
qmap(location=c(37.6150, 55.7610), zoom = 13, maptype = 'roadmap', 
     base_layer = ggplot(aes(x = startLon, y = startLat), data = var_ways)) +
  geom_leg(aes(x = startLon, xend = endLon, 
               y = startLat, yend = endLat, color = route), 
           alpha = 0.5, size = 2, data = var_ways) +
  labs(x = 'Longitude', y = 'Latitude', colour = 'Route') +
  facet_wrap(~ route, ncol = 3) + theme(legend.position = 'top')
Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=55.761,37.615&zoom=13&size=640x640&scale=2&maptype=roadmap&language=en-EN&sensor=false
Warning message:
“`panel.margin` is deprecated. Please use `panel.spacing` property instead”
In [28]:
options(repr.plot.width=10, repr.plot.height=10)
plot(bpavposad)
In [29]:
ts <- find(bpavposad, node(tags(v == "traffic_signals")))
ts_pp <- subset(bpavposad, node_ids = ts)
bs <- find(bpavposad, node(tags(v %agrep% "busstop")))
bs_pp <- subset(bpavposad, node_ids = bs)
hw <- find(bpavposad, way(tags(k == "highway")))
hw <- find_down(bpavposad, way(hw))
hw_pp <- subset(bpavposad, ids = hw)
tu <- find(bpavposad, way(tags(k == "tunnel")))
tu <- find_down(bpavposad, way(tu))
tu_pp <- subset(bpavposad, ids = tu)
In [30]:
options(repr.plot.width=14, repr.plot.height=14)
plot_ways(hw_pp, col = "steelblue")
plot_ways(tu_pp, add = TRUE, col = "magenta")
plot_nodes(ts_pp, add = TRUE, col = "red")
plot_nodes(bs_pp, add = TRUE, col = "blue")
In [31]:
bg <- find(bpavposad, way(tags(k == "building")))
bg <- find_down(bpavposad, way(bg))
bg_pp <- subset(bpavposad, ids = bg)
bg_poly <- as_sp(bg_pp, "polygons")
In [32]:
spplot(bg_poly, col.regions=brewer.pal(12, "Set3"), c("version"))
In [33]:
bs_points <- as_sp(bs_pp, "points")
hw_line <- as_sp(hw_pp, "lines")
In [34]:
plot(bg_poly, col = "lightsteelblue")
plot(hw_line, add = TRUE, col = "blue")
plot(bs_points, add = TRUE, col = "red")

📑   Plotting

In [25]:
pl1 <- plot_ly(n_data, x=n_data[1:20,1], y=n_data[1:20,2], z=n_data[1:20,3], color = n_data[1:20,4], 
               text = paste("random variable: ", n_data[1:20,4]), 
               colors = 'Spectral', type="scatter3d", mode="markers", 
               marker = list(size = 10, symbol = "diamond-open"))
pl2 <- layout(pl1, title = "Random Matrix",
         scene = list(xaxis = list(title = "var1"), yaxis = list(title = "var2"), zaxis = list(title = "var3")))
In [26]:
pl2
In [27]:
x <- rnorm(200)
y <- rchisq(200, df = 1, ncp = 0)
group <- sample(LETTERS[1:5], size = 200, replace = T)
size <- sample(1:5, size = 200, replace = T)

ds <- data.frame(x, y, group, size)

pl3 <- plot_ly(ds, type = "scatter", x = x, y = y, mode = "markers", 
               color=group, size = size, opacity=0.7, colors = 'PuRd')
pl4 <- layout(pl3, title = "Scatter Plot")
In [28]:
pl4
In [46]:
options(repr.plot.width=5, repr.plot.height=5)
p15 <- pie3D(x2, labels = l2, explode = 0.2, main = "Example Pie Chart")
p15
  1. 1.22522113490002
  2. 3.11645991236107
  3. 4.39508812237212
  4. 5.31557476987393
  5. 5.85592870629137
  6. 6.1857959349183
In [ ]:

In [ ]:

In [ ]:

📑   Graphs

In [32]:
g <- graph_from_adjacency_matrix(A, mode = "undirected")
In [33]:
options(repr.plot.width=14, repr.plot.height=7)
plot(g, layout=layout_with_fr,  
     vertex.label.dist=1.5, vertex.label.color='black',
     vertex.size=10, vertex.shape='csquare', vertex.color='steelblue', 
     edge.color='darkblue', edge.arrow.size=2.5)
In [34]:
shortest_paths(g, 4, 12)
$vpath
$vpath[[1]]
+ 6/15 vertices:
[1]  4  2  1  3  6 12


$epath
NULL

$predecessors
NULL

$inbound_edges
NULL
In [35]:
options(repr.plot.width=14, repr.plot.height=7)
g1 <- sample_gnp(60, 1/50)
comps <- components(g1)$membership
colbar <- rainbow(max(comps)+1)
V(g1)$color <- colbar[comps+1]
plot(g1, layout=layout_with_fr, vertex.size=4,
    vertex.label.dist=0.7, vertex.label.color='black')
In [30]:
nodes <- data.frame(id = 1:9, title = paste("node", 1:9), 
                    shape = c("dot", "square", "square"),
                    size = 11:19, color = c("blue", "red", "purple"))
edges <- data.frame(from = 1:9, to = c(2, 3, 1, 1, 2, 3, 4, 5, 6))
visNetwork(nodes, edges) %>%
  visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE)
In [46]:
nodes
idtitleshapesizecolor
1 node 1dot 11 blue
2 node 2square12 red
3 node 3square13 purple
4 node 4dot 14 blue
5 node 5square15 red
6 node 6square16 purple
7 node 7dot 17 blue
8 node 8square18 red
9 node 9square19 purple
In [47]:
edges
fromto
12
23
31
41
52
63
74
85
96
In [ ]: